package org.jeecg.modules.yw.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.entity.EnterpriseStatic;
import org.jeecg.modules.yw.service.EnterpriseStaticService;
import org.jeecg.modules.yw.utils.NumberUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@RestController
@Api(tags = "统计管理")
@RequestMapping("/orderView")
public class EnterpriseStaticController extends JeecgController<EnterpriseStatic, EnterpriseStaticService> {

    @Autowired
    private EnterpriseStaticService enterpriseStaticService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "门窗企业统计")
    @GetMapping("/enterprise")
    public Result enterprise(@RequestParam(value = "start", required = false) String start,
                             @RequestParam(value = "end", required = false) String end,
                             @RequestParam(value = "name", required = false)String name,
                             @RequestParam(name = "pageNo") Integer pageNo,
                             @RequestParam(name = "pageSize") Integer pageSize ) {
        Page<EnterpriseStatic> page= new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        String[] areas=null;
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
                areas=area.split(",");
            }else{
                IPage<EnterPrise> result =new Page<>();
                return org.jeecg.common.api.vo.Result.ok(result);
            }
        }
        return Result.ok (enterpriseStaticService.pageEnterprise(page,start,end,name,areas));
    }
    @ApiOperation(value = "导出门窗企业统计")
    @GetMapping("/export")
    public ModelAndView export(HttpServletRequest request,
                               @RequestParam(value = "start", required = false) String start,
                               @RequestParam(value = "end", required = false) String end,
                               @RequestParam(value = "name", required = false)String name,
                               @RequestParam(value = "ids", required = false) String ids ) {
        String[] names = null;
        List<EnterpriseStatic> result =null;
        if (!StringUtils.isEmpty(ids)){
            names = ids.split(",");
            result=new ArrayList<>(names.length);
            for (String id : names) {
                String[] str = id.split("@@",-1);
                EnterpriseStatic enterpriseStatic = new EnterpriseStatic();
                enterpriseStatic.setName(str[0]);
                enterpriseStatic.setContractEnd(DateUtils.strToDateTime(str[1]));
                enterpriseStatic.setLinkman(str[2]);
                enterpriseStatic.setMobile(str[3]);
                enterpriseStatic.setHjmj(NumberUtils.strToDouble(str[4]));
                enterpriseStatic.setJsje(NumberUtils.strToDouble(str[5]));
                enterpriseStatic.setYfk(NumberUtils.strToDouble(str[6]));
                enterpriseStatic.setWfk(NumberUtils.strToDouble(str[7]));
                result.add(enterpriseStatic);
            }
        }else {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
            String[] areas=null;
            if (!roleIds.contains(CommonUtils.ADMIN)) {
                String area=sysUser.getArea();
                if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
                    areas=area.split(",");
                }else{
                    String user= UserUtil.getUserName(request);
                    return super.exportXls(new ArrayList<>(), EnterpriseStatic.class,user, "考勤审批");
                }
            }
            result = enterpriseStaticService.listEnterprise(start, end, name,areas);
        }
        String user= UserUtil.getUserName(request);
        return super.exportXls(result, EnterpriseStatic.class,user, "考勤审批");
    }
}
